#pragma once
#include "DXAMLCommon.h"
#include "IErrorService.h"

#include "ICoreServices.h"
#include "IPALCriticalSection.h"
#include "CXcpList.h"
#include "IError.h"
#include "IErrorServiceListener.h"

//throw std::logic_error("The method or operation is not implemented.");
class CErrorService :
	public IErrorService
{

public: 
	virtual XUINT32 AddRef();
	virtual XUINT32 Release();
	virtual HRESULT AddListener(struct IErrorServiceListener * pListener);
	virtual HRESULT RemoveListener(struct IErrorServiceListener * pListener);
	virtual HRESULT GetLastReportedError(struct IError * * ppError);
	virtual HRESULT GetFirstError(struct IError * * ppError);
	virtual HRESULT AddError(struct IError * pErrorObject);
	virtual HRESULT CleanupLastError(struct IError * pError);
	virtual void CleanupErrors();
	virtual void CoreResetCleanup(struct ICoreServices * pCore);

	static HRESULT UpdateErrorCodeFromXResult(HRESULT xResult, XUINT32 * pErrorCode);
	static HRESULT UnspecifyMediaError(XUINT32, XUINT32 *);

	virtual HRESULT GetMessageFromErrorCode(XUINT32 iErrorID, XCHAR * * ppMessage, XUINT32 * pcMessage);

	static HRESULT FormatErrorMessage(XCHAR * pOriginalErrorMessage, XUINT32 cOriginalErrorMessage, XCHAR * * ppFormattedErrorMessage, XUINT32 * pcFormattedErrorMessage, struct XSTRING * * ppParam, XUINT32 cParams);

	virtual HRESULT ReportGenericError(enum ErrorType eType, XUINT32 iErrorCode, XBOOL bRecoverable, XUINT32 uLineNumber, XUINT32 uCharPosition, struct XSTRING * * ppParam, XUINT32 cParams, struct Xcp_IDependencyObject * pErrorEventArgs, struct Xcp_IDependencyObject * pSender);
	virtual HRESULT ReportParserError(XUINT32 iErrorCode, XBOOL bRecoverable, CXString * pstrXamlFileName, CXString * pstrXmlElement, CXString * pstrXmlAttribute, XUINT32 uLineNumber, XUINT32 uCharPosition, CXString * pstrMessage, struct XSTRING * * ppParam, XUINT32 cParams);
	virtual HRESULT ReportRuntimeError(XUINT32 iErrorCode, XBOOL bRecoverable, XUINT32 uLineNumber, XUINT32 uCharPosition, CXString * pstrMethodName, struct XSTRING * * ppParam, XUINT32 cParams, struct Xcp_IDependencyObject * pErrorEventArgs, struct Xcp_IDependencyObject * pSender, XUINT32 cMessage, XCHAR * pErrorMessage);
	virtual HRESULT ReportManagedError(XUINT32 bRecoverable, XBOOL cMessage, XCHAR * pErrorMessage);


	static HRESULT ReportInvalidOperationError(struct ICoreServices * pCore, XUINT32 uiErrorCode, CXString * pstrParameter1, CXString * pstrParameter2, CXString * pstrParameter3);
	static HRESULT Create(struct ICoreServices * pCore, struct IErrorService * * ppErrorService);

protected: 
	CErrorService(struct ICoreServices * pCore);
	~CErrorService();
	long Initialize();
private: 
	void CleanupListeners();
private: 
	struct IPALCriticalSection * m_pCSError;
	struct ICoreServices * m_pCore;
	XUINT32 m_cRef;
	CXcpList<IError> * m_pErrorList;
	CXcpList<IErrorServiceListener> * m_pListenerList;


};


